#ifndef _UBT_STATE_TASK_ADJUST_H_
#define _UBT_STATE_TASK_ADJUST_H_
#include "ubt_state_types.h"
#include "ubt_state_api.h"
#include "ubt_interface/msg/ubt_adjust_msg.hpp"
#include "ubt_com/ubt_proto_adjust.h"
#include <map>
#include "ubt_com/cJSON.h"
#include "std_msgs/msg/int64.hpp"
#include "std_msgs/msg/string.hpp"

namespace ubt_state
{
class CTaskAdjust: public ITask
{
  public:
    static int ClassInit();
    static void ClassCleanup();
    /*
    	poseType=1 odomrel(里程相对)， 2 odomabs(里程绝对),3 map(地图坐标),4 qrcode(二维码) ,5 rack(货架),6 qrcode1(上方二维码);
    	poseDst,avoidPose都是poseType坐标系
    */
    static ITask *CreateTask(ITaskCb *pTaskCb, std::string taskId, ubt_task_stage stage, int timeOutMs,
                             ubt_proto_adjust::ubt_adjust_pose_type poseType, geometry_msgs::msg::Pose2D poseDst, geometry_msgs::msg::Pose2D poseDstLmt,
                             float stopDis, float sideDis, int avoidHwMask,
                             int index, int reportIndex, std::string actionType);

    static ITask *CreateTask(ITaskCb *pTaskCb, std::string taskId, ubt_task_stage stage, bool isRechargeAdjust, int index, int reportIndex);

    ~CTaskAdjust();

    bool Init(ITaskCb *pTaskCb, std::string taskId,  ubt_task_stage stage, int timeOutMs,
              ubt_proto_adjust::ubt_adjust_pose_type poseType, geometry_msgs::msg::Pose2D poseDst, geometry_msgs::msg::Pose2D poseLmt,
              float stopDis, float sideDis, int avoidHwMask,
              int index, int reportIndex, std::string actionType);
    
    bool Init(ITaskCb *pTaskCb, std::string taskId,  ubt_task_stage stage, bool isRechargeAdjust, int index, int reportIndex);

    virtual bool Start();
    virtual void Pause();
    virtual void Continue();
    virtual void Stop(int reason);
    virtual void OnTimer();
    virtual void OnMsg(int msgType, void *pvMsg);

    virtual ubt_task_type GetType();
    virtual ubt_task_state GetState();
    virtual ubt_task_stage GetStage();
    virtual std::string GetActionType();
    virtual void GetIndex(int& index, int& reportIndex);
    virtual void GetStopInfo(int& stopReason, std::string& stopReasonDesc);
    virtual void GetLogs(std::vector<TaskLogItem>& logs);


    void Sub_Adjust_Callback(const ubt_interface::msg::UbtAdjustMsg::SharedPtr msg);

    void Sub_V_Recharge_Callback(const std_msgs::msg::Int64::SharedPtr msg);

  private:
    int _SendMsg(int msgType, void *pvMsg);
    bool _SendMsg(int mode, std::string title, int sessionId);
    void _Stop(int reason);
    void _SetState(ubt_task_state newState, int reason, std::string reasonStr);
    void _OnLocationCb();
    void _OnAvoidStatusCb();
    void _CheckPause(int event);

    ITaskCb *_pTaskCb;
    std::string _taskId;
    int _index;
    int _reportIndex;
    std::string _actionType;
    ubt_task_stage _stage;
    ubt_proto_adjust::ubt_adjust_pose_type _poseType;
    geometry_msgs::msg::Pose2D _poseDst;
    geometry_msgs::msg::Pose2D _poseLmt;
    int _timeOutMs;
    float _stopDis;
    float _sideDis;
    int _avoidHwMask;//avoid_hw_mask_xx

    ubt_task_state _state;
    long _stateTime;
    int _msgId;
    int _avoidHwStatus;
    int _avoidDisable;
    int _pauseState;
    int _stopReason;
    std::string _stopReasonDesc;

    bool _isRechargeAdjust = false;

    std::map<int, std::string> rechargeResultCodeMap = {
      {7000, "RECHARGE_FINISHED"},
      {7001, "RECHARGE_INIT_FAILED"},
      {7002, "RECHARGE_NOPILE"},
      {7003, "RECHARGE_TIMEOUT"},
      {7004, "RECHARGE_TO_PILE_FAILED"},
      {7100, "RECHARGE_UNKNOWN_FAILED"},
      {7010, "RECHARGE_GO_OFF_FINISHED"},
      {7011, "RECHARGE_GO_OFF_FAILED"},
      {1000, "RECHARGE_COMMAND_ACCEPTED"}
    };

};
}
#endif

